package com.v1.video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.pulltorefresh.PullToRefreshBase;
import com.iss.view.pulltorefresh.PullToRefreshBase.Mode;
import com.iss.view.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.iss.view.pulltorefresh.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.adapter.ThemeDetailAdapter;
import com.v1.video.db.dao.MyThemeTB;
import com.v1.video.db.dao.MyThemeVideoTB;
import com.v1.video.db.service.ThemeMyThemeDB;
import com.v1.video.domain.ThemeDetailEntity;
import com.v1.video.domain.ThemeDetailEntity.ThematicInfo;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;

import java.util.ArrayList;
import java.util.List;
/**
 * 2016.3.28
 * 主题详情
 * @author wangf
 *
 */
public class ThemeDetailActivity extends BaseActivity implements OnClickListener {

	private final String TAG = "ThemeDetailActivity";
	private View mViewHead;
	private PullToRefreshListView mRefreshListView;
	private ListView mListView;
	private TextView mTvTitle;
	private String mThemeId;
	private String title = "";
	private ThemeDetailAdapter mAdapter;
	private int mPageIndex = 1;
	private TextView txtGuanZhu = null;
	private List<ThematicInfo> mLstData = new ArrayList<ThemeDetailEntity.ThematicInfo>();
	private String strBackUrl = "";
	private ImageView themeImg = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_theme_detail);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mViewHead = getLayoutInflater().inflate(
				R.layout.activity_theme_detail_header, null);
		mTvTitle = (TextView) this.findViewById(R.id.tv_title);
		mRefreshListView = (PullToRefreshListView) this
				.findViewById(R.id.theme_detail_listView);
		mRefreshListView.setScrollingWhileRefreshingEnabled(false);
		mRefreshListView.setMode(Mode.BOTH);
		mListView = mRefreshListView.getRefreshableView();
		mListView.addHeaderView(mViewHead);
		txtGuanZhu = (TextView)mViewHead.findViewById(R.id.item_theme_detail_btn_guanzhu);		
		themeImg = (ImageView)mViewHead.findViewById(R.id.theme_detail_image);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = this.getIntent();
		title = intent.getStringExtra("title");
		mThemeId = intent.getStringExtra("themdId");
//		strBackUrl = intent.getStringExtra("themeImg");
//		String content = intent.getStringExtra("content");
		if (!TextUtils.isEmpty(title)) {
			mTvTitle.setText(title);
		}
		MyThemeTB mThemeTb = ThemeMyThemeDB.getInstance().getSingMyThemeData(mThemeId);
		if(mThemeTb != null && mThemeTb.getThemeID().equals(mThemeId)){
			txtGuanZhu.setText("已关注");
			txtGuanZhu.setTextColor(getResources().getColor(R.color.color_cccccc));
			txtGuanZhu.setBackgroundResource(R.drawable.shape_yiguanzhu_bg);
		}else{
			txtGuanZhu.setText("关注");
			txtGuanZhu.setTextColor(getResources().getColor(R.color.color_007dff));
			txtGuanZhu.setBackgroundResource(R.drawable.shape_guanzhu_bg);
		}
		mAdapter = new ThemeDetailAdapter(this);
		mListView.setAdapter(mAdapter);
		if (Helper.checkConnection(ThemeDetailActivity.this)) {
			getServerData();
		} else {
			Toast.makeText(ThemeDetailActivity.this,
					getResources().getString(R.string.net_no_notclick),
					Toast.LENGTH_SHORT).show();
		}
		
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd("主题详情页");
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart("主题详情页");
		MobclickAgent.onResume(this);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.lay_result).setOnClickListener(this);
		txtGuanZhu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MyThemeTB mThemeTb = ThemeMyThemeDB.getInstance().getSingMyThemeData(mThemeId);
				if(mThemeTb != null && mThemeTb.getThemeID().equals(mThemeId)){
					txtGuanZhu.setText("关注");
					txtGuanZhu.setTextColor(getResources().getColor(R.color.color_007dff));
					txtGuanZhu.setBackgroundResource(R.drawable.shape_guanzhu_bg);
					
					List<MyThemeVideoTB> list = ThemeMyThemeDB.getInstance().getMyThemdVideoFromMyTheme(mThemeId);
					ThemeMyThemeDB.getInstance().deleteInTx(list);
					ThemeMyThemeDB.getInstance().delete(mThemeTb);
				}else{
					txtGuanZhu.setText("已关注");
					txtGuanZhu.setTextColor(getResources().getColor(R.color.color_cccccc));
					txtGuanZhu.setBackgroundResource(R.drawable.shape_yiguanzhu_bg);
					MyThemeTB theme = new MyThemeTB();
					theme.setThemeID(mThemeId);
					theme.setThemeName(title);
					theme.setStatus("1");
					ThemeMyThemeDB.getInstance().insertOrReplaceMyTheme(theme);
					List<MyThemeVideoTB> myThemeVideoList = new ArrayList<MyThemeVideoTB>();
					int length = 3;
					if(mLstData.size() < 3){
						length = mLstData.size();
					}
					for(int i = 0; i < length;i++){
						MyThemeVideoTB video = new MyThemeVideoTB();
						video.setVid(mLstData.get(i).getVid());
						video.setDuration(mLstData.get(i).getDuration());
						video.setAt(mLstData.get(i).getAt());
						video.setPic(mLstData.get(i).getPic());
						video.setTitle(mLstData.get(i).getTitle());
						video.setThemeID(mThemeId);
						myThemeVideoList.add(video);
					}
					ThemeMyThemeDB.getInstance().insertMyThemeVideo(myThemeVideoList);
				}
			}
		});
		mRefreshListView.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(ThemeDetailActivity.this)) {
					mPageIndex = 1;
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					Toast.makeText(ThemeDetailActivity.this,
							getResources().getString(R.string.net_no_notclick),
							Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(ThemeDetailActivity.this)) {
					++mPageIndex;
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					Toast.makeText(ThemeDetailActivity.this,
							getResources().getString(R.string.net_no_notclick),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		default:
			break;
		}
	}

	/**
	 * 获取服务器数据
	 */
	private void getServerData() {
		String url = String.format(Constant.THEME_DETAIL_VIDEO_LIST, mThemeId,
				mPageIndex, Constant.PRODUCT_CODE, Constant.PRODUCT_VERSION);
		Logger.i(TAG, "佛教详情页url="+url);
		RequestManager.getInstance().getRequest(this, url,
				ThemeDetailEntity.class, new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						if (mRefreshListView != null) {
							mRefreshListView.onRefreshComplete();
						}
						if (object != null) {
							try {
								ThemeDetailEntity entity = (ThemeDetailEntity) object;
								if (entity != null && entity.getBody() != null
										&& entity.getBody().getData() != null) {
									mLstData.addAll(entity.getBody().getData());
									String strPageTotal = entity.getBody().getPage();
									if(strPageTotal != null && strPageTotal.length() > 0){
										
									}else{
										strPageTotal = "1";
									}
									int pageTotal = Integer.parseInt(strPageTotal);
									if (mPageIndex < pageTotal) {
										if (mRefreshListView != null) {
											mRefreshListView.setMode(Mode.BOTH);
										}
									} else {
										if (mRefreshListView != null) {
											mRefreshListView
													.setMode(Mode.PULL_DOWN_TO_REFRESH);
										}
									}
									if (mAdapter != null) {
										if (mPageIndex == 1) {
											mAdapter.setLstData(entity
													.getBody().getData());
											strBackUrl = entity.getBody().getThemeImg();
											ImageLoader.getInstance().displayImage(strBackUrl, themeImg,
													Constant.THEME_IMG_ICON, null);
										} else {
											mAdapter.setAddLstData(entity
													.getBody().getData());
										}
									}
								}
							} catch (Exception ex) {
								ex.printStackTrace();
								Logger.e(TAG, ex.getMessage());
							}
						}
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						if (mRefreshListView != null) {
							mRefreshListView.onRefreshComplete();
						}
						Toast.makeText(ThemeDetailActivity.this, message,
								Toast.LENGTH_SHORT).show();
					}
				});
	}
}
